import React, { Component } from 'react'
import Far from './Far'
import PubSub from 'pubsub-js'
export default class App extends Component {
  componentDidMount() {
    // 订阅话题
    this.id1 = PubSub.subscribe('richFemale', (topic, data) => {
      console.log('app组件接收到了富婆的话题', topic, data)
    })

    PubSub.subscribe('car', (topic, data) => {
      console.log('app组件接收到了汽车的话题', topic, data)
    })
  }
  componentWillUnmount() {
    // 按照实际开发规范,挂载的时候订阅,卸载的时候就一定要清除指定订阅. 但是我们看不到效果,所以一会儿会专门写几个按钮,给大家演示效果
    PubSub.unsubscribe(this.id1)
  }
  render() {
    return (
      <div>
        App
        <br />
        <button
          onClick={() => {
            PubSub.unsubscribe(this.id1)
          }}
        >
          清除app中订阅富婆的指定话题
        </button>
        <button
          onClick={() => {
            PubSub.unsubscribe('richFemale')
          }}
        >
          清除富婆的订阅
        </button>
        <button
          onClick={() => {
            PubSub.clearAllSubscriptions()
          }}
        >
          清除所有订阅
        </button>
        <br />
        <Far></Far>
      </div>
    )
  }
}
